<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tangly.codeing.dao.${classInfo.className}Dao">

    <resultMap id="${classInfo.className}" type="com.tangly.codeing.model.${classInfo.className}" >
    <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
    <#list classInfo.fieldList as fieldItem >
        <result column="${fieldItem.columnName}" property="${fieldItem.fieldName}" />
    </#list>
    </#if>
    </resultMap>

    <sql id="Base_Column_List">
    <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
    <#list classInfo.fieldList as fieldItem >
        `${fieldItem.columnName}`<#if fieldItem_has_next>,</#if>
    </#list>
    </#if>
    </sql>

    <insert id="insert" parameterType="com.tangly.codeing.model.${classInfo.className}" >
        INSERT INTO ${classInfo.tableName}
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list classInfo.fieldList as fieldItem >
        <#if fieldItem.columnName != "Id" >
            <if test="${fieldItem.fieldName} != null">
                `${fieldItem.columnName}`<#if fieldItem_has_next>,</#if>
            </if>
        </#if>
        </#list>
        </trim>
        </#if>
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list classInfo.fieldList as fieldItem >
        <#if fieldItem.columnName != "Id" >
            <#if fieldItem.columnName="AddTime" || fieldItem.columnName="UpdateTime" >
            NOW()<#if fieldItem_has_next>,</#if>
            <#else>
            <if test="${fieldItem.fieldName} != null">
                ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
            </if>
            </#if>
        </#if>
        </#list>
        </trim>
        </#if>
    </insert>

    <delete id="delete" parameterType="java.util.Map" >
        UPDATE ${classInfo.tableName} SET status = 0
        WHERE `id` = ${r"#{id}"}
    </delete>

    <delete id="batchDelete">
        update ${classInfo.tableName} set status = 0
        where id in
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            ${r"#{id}"}
        </foreach>
    </delete>

    <update id="update" parameterType="com.tangly.codeing.model.${classInfo.className}" >
        UPDATE ${classInfo.tableName}
        SET
        <#list classInfo.fieldList as fieldItem >
        <#if fieldItem.columnName != "Id" && fieldItem.columnName != "AddTime" && fieldItem.columnName != "UpdateTime" >
            <if test="${fieldItem.fieldName} != null">
                ${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
            </if>
        </#if>
        </#list>
        WHERE `id` = ${r"#{"}id${r"}"}
    </update>


    <select id="load" parameterType="java.lang.Long" resultMap="${classInfo.className}">
        SELECT <include refid="Base_Column_List" />
        FROM ${classInfo.tableName}
        WHERE `id` = ${r"#{id}"}
    </select>

    <select id="pageList" parameterType="java.util.Map" resultMap="${classInfo.className}">
        SELECT <include refid="Base_Column_List" />
        FROM ${classInfo.tableName}
        WHERE status != 0
        <include refid="whereMapSql"/>
        <include refid="orderLimitSql"/>
    </select>

    <select id="pageListCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(1)
        FROM ${classInfo.tableName}
        WHERE status != 0
        <include refid="whereMapSql"/>
    </select>

    <sql id="whereMapSql">
        <if test="whereMap != null">
            <if test="whereMap.loginName != null and whereMap.loginName != ''">
                and login_name = ${r"#{whereMap.loginName}"}
            </if>
            <if test="whereMap.password != null and whereMap.password != ''">
                and password = ${r"#{whereMap.password}"}
            </if>
        </if>
    </sql>

    <sql id="orderLimitSql">
        <if test="orderColumn != null and orderSort != null">
            order by ${r"${orderColumn} ${orderSort}"}
        </if>
        <if test="orderColumn == null or orderSort == null">
            order by id asc
        </if>
        <if test="startRecord != null and pageSize != null">
            limit ${r"#{startRecord,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}"}
        </if>
    </sql>
</mapper>
